#!/usr/bin/env bash

################################################################################
#
# Copyright (C) 2013  Artur Tolstenco
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
################################################################################

# =============================================================================
#
# Author:       Artur Tolstenco
# Date:		    15/05/2013
# Description:	This script creates (or deletes) a new mysql user and its database. 
#               It also populates the database with suitable data depending on 
#               where it is deployed - on dione server or on a localmachine
#
# =============================================================================

CURRENT_DIR=$(dirname "$0")
EXPECTED_ARGS=2
CLEAN_EXPECTED_ARGS=2
CREATE_EXPECTED_ARGS=3

PAR_CREATE='create'
PAR_CLEAN='clean'

PAR_DEPLOY_ON_DIONE='dione'
PAR_DEPLOY_ON_LOCAL='local'

PAR_ALL='all'
PAR_CREATE_USER='create_user'
PAR_POPULATION='populate'


DB_USER='authserver'
DB_USER_PWD='random85'
DATABASE_TO_CREATE='authserver_zf'

DB_SCHEMA=$CURRENT_DIR'/schema.mysql.sql'

# constants for dione
DB_DEFAULT_DATA_ON_DIONE=$CURRENT_DIR'/dione.data.mysql.sql'
DB_ROOT_ON_DIONE='root'
DB_ROOT_PWD_ON_DIONE='random85'
DB_HOST_ON_DIONE='localhost'

# constants for local machine
DB_DEFAULT_DATA_ON_LOCAL=$CURRENT_DIR'/data.mysql.sql'
DB_ROOT_ON_LOCAL='root'
DB_ROOT_PWD_ON_LOCAL='toor'
DB_HOST_ON_LOCAL='localhost'

# current deploy configuration - will be modified
DB_DEFAULT_DATA=''
DB_ROOT=''
DB_ROOT_PWD=''
DB_HOST=''


# PRIVATE FUNCTIONS ############################################################

# checks if there was an error during execution of previous comand 
# and prints the $1 message
function check_error() {
    if [ $? -gt 0 ]; then
		echo $1
		exit 1
	fi
}

function check_files() {
    if [ ! -f $DB_SCHEMA ] || [ ! -f $DB_DEFAULT_DATA ]; then
        echo 'The population files for "'$DATABASE_TO_CREATE'" database does not exist'
        exit 1
    fi
}

function set_deployment_environment() {
    case $1 in
        $PAR_DEPLOY_ON_DIONE)
            DB_DEFAULT_DATA=$DB_DEFAULT_DATA_ON_DIONE
            DB_ROOT=$DB_ROOT_ON_DIONE
            DB_ROOT_PWD=$DB_ROOT_PWD_ON_DIONE
            DB_HOST=$DB_HOST_ON_DIONE
            ;;
        $PAR_DEPLOY_ON_LOCAL)
            DB_DEFAULT_DATA=$DB_DEFAULT_DATA_ON_LOCAL
            DB_ROOT=$DB_ROOT_ON_LOCAL
            DB_ROOT_PWD=$DB_ROOT_PWD_ON_LOCAL
            DB_HOST=$DB_HOST_ON_LOCAL
            ;;
        *)
            echo 'Unrecognized parameter. Allowed parameters: '$PAR_DEPLOY_ON_DIONE', '$PAR_DEPLOY_ON_LOCAL
            exit 1
    esac
}

# creates a new mysql database user
function create_user() {
    bash $CURRENT_DIR/db_user.sh create $DB_ROOT $DB_ROOT_PWD $DB_HOST \
	                                     $DB_USER $DB_USER_PWD \
	                                     $DATABASE_TO_CREATE
    check_error 'Error creating the new user or its database...'
	echo 'Created the new user and its databases successfuly'
}

# populates the $DATABASE_TO_CREATE
function populate() {
    check_files
    php $CURRENT_DIR/load.mysql.php -s $DB_SCHEMA -d $DB_DEFAULT_DATA -w
    check_error 'Error during the database population...'
    echo 'Databases populated successfully'
    
    echo
    echo '================================================================================'
    echo 'The database connection params must be edited in the application.ini:'
    echo 'username = '$DB_USER
    echo 'password = '$DB_USER_PWD
    echo 'host = '$DB_HOST
    echo 'dbname = '$DATABASE_TO_CREATE
    echo '================================================================================'
    echo
}

function all() {
    create_user
    populate
}

function create() {
    case $1 in
        $PAR_CREATE_USER)
            create_user
            ;;
        $PAR_POPULATION)
            populate
            ;;
        $PAR_ALL)
            all
            ;;
        *)
            echo 'Unrecognized parameter. Allowed parameters: '$PAR_ALL', '$PAR_CREATE_USER', '$PAR_POPULATION
            exit 1
    esac
}

function clean() {    
    bash $CURRENT_DIR/db_user.sh delete $DB_ROOT $DB_ROOT_PWD $DB_HOST \
	                                     $DB_USER $DB_USER_PWD \
	                                     $DATABASE_TO_CREATE
    check_error 'Error deleting user and its databases...'
	echo 'The mysql db user and its database was deleted successfully'
}

################################################################################


# MAIN #########################################################################

if [ $# -lt $EXPECTED_ARGS ]; then
    echo 'Error: some arguments are missing'
	echo 'Usage: '$(basename $0)' '$PAR_CLEAN' '$PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL
    echo 'Usage: '$(basename $0)' '$PAR_CREATE\
                                   $PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL\
                                   $PAR_ALL'|'$PAR_CREATE_USER'|'$PAR_POPULATION
	exit 1
fi

set_deployment_environment $2

case $1 in
    $PAR_CLEAN)
        if [ $# -ne $CLEAN_EXPECTED_ARGS ]; then
	        echo 'Error: some arguments are missing'
	        echo 'Usage: '$(basename $0)' '$PAR_CLEAN' '$PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL
	        exit 1
        fi
        
        clean
        ;;
    $PAR_CREATE)
        if [ $# -ne $CREATE_EXPECTED_ARGS ]; then
	        echo 'Error: some arguments are missing'
	        echo 'Usage: '$(basename $0)' '$PAR_CREATE'|'$PAR_CLEAN\
	                                       $PAR_DEPLOY_ON_DIONE'|'$PAR_DEPLOY_ON_LOCAL\
	                                       $PAR_ALL'|'$PAR_CREATE_USER'|'$PAR_POPULATION
	        exit 1
        fi
        
        create $3
        ;;
    *)
        echo 'Unrecognized parameter. Allowed parameters: '$PAR_CREATE', '$PAR_CLEAN
esac

exit 0
